/**
 * 用户路由
 * 定义用户相关的API路由
 */
const express = require('express');
const userController = require('../controllers/userController');
const authMiddleware = require('../middlewares/auth');

const router = express.Router();

/**
 * @swagger
 * /login: 
 *   post:
 *     summary: 用户登录
 *     tags: [用户]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名，长度为3-20个字符，只能包含字母、数字和下划线
 *                 minLength: 3
 *                 maxLength: 20
 *                 pattern: '^[a-zA-Z0-9_]+$'
 *                 example: 'zhangsan'
 *               password:
 *                 type: string
 *                 description: 密码，长度为6-20个字符，必须包含字母和数字
 *                 minLength: 6
 *                 maxLength: 20
 *                 pattern: '^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$'
 *                 example: 'Password123'
 *     responses:
 *       200:
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 用户名或密码错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 用户登录 - 不需要认证
router.post('/login', userController.login);

/**
 * @swagger
 * /wxLogin: 
 *   post: 
 *     summary: 微信小程序登录
 *     tags: [用户]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               code:
 *                 type: string
 *                 description: 微信小程序登录code，通过wx.login()获取
 *                 example: '013wx000000hQd1234567890ABCDEF'
 *     responses:
 *       200: 
 *         description: 登录成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *             examples:
 *               success: 
 *                 summary: 登录成功示例
 *                 value: 
 *                   code: '200'
 *                   msg: '操作成功'
 *                   data: 
 *                     user: 
 *                       id: 1
 *                       username: 'wx_user_1234567890'
 *                       phone: null
 *                       registerTime: '2023-10-15T08:30:00Z'
 *                     token: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...'
 *       400: 
 *         description: 参数错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *             examples:
 *               paramError: 
 *                 summary: 参数错误示例
 *                 value: 
 *                   code: '400'
 *                   msg: 'code不能为空'
 *                   data: null
 *       500: 
 *         description: 系统错误
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *             examples:
 *               systemError: 
 *                 summary: 系统错误示例
 *                 value: 
 *                   code: '500'
 *                   msg: '微信登录失败: 无效的appid'
 *                   data: null
 */
// 微信小程序登录 - 不需要认证
router.post('/wxLogin', userController.wxLogin);

/**
 * @swagger
 * /register: 
 *   post:
 *     summary: 用户注册
 *     tags: [用户]
 *     requestBody:
 *       required: true
 *       content:
 *         application/json:
 *           schema:
 *             type: object
 *             properties:
 *               username:
 *                 type: string
 *                 description: 用户名，长度为3-20个字符，只能包含字母、数字和下划线
 *                 minLength: 3
 *                 maxLength: 20
 *                 pattern: '^[a-zA-Z0-9_]+$'
 *                 example: 'zhangsan'
 *               password:
 *                 type: string
 *                 description: 密码，长度为6-20个字符，必须包含字母和数字
 *                 minLength: 6
 *                 maxLength: 20
 *                 pattern: '^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]+$'
 *                 example: 'Password123'
 *     responses:
 *       200:
 *         description: 注册成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       400:
 *         description: 用户名已存在或参数不符合要求
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 用户注册 - 不需要认证
router.post('/register', userController.register);

/**
 * @swagger
 * /current: 
 *   get:
 *     summary: 获取当前登录用户信息
 *     tags: [用户]
 *     security:
 *       - bearerAuth: []
 *     responses:
 *       200:
 *         description: 获取成功
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 *       401:
 *         description: 未授权，需要先登录
 *         content:
 *           application/json:
 *             schema:
 *               $ref: '#/components/schemas/ApiResponse'
 */
// 获取当前登录用户信息 - 需要认证
router.get('/current', authMiddleware, userController.getCurrentUser);

module.exports = router;